package com.jet.goods.controller;

import com.jet.core.model.vo.PageVo;
import com.jet.core.result.ResultJson;
import com.jet.core.enums.CommonDeleted;
import com.jet.goods.domain.dto.CategoryDto;
import com.jet.goods.domain.entity.GoodsCategory;
import com.jet.goods.domain.enums.CategoryLevel;
import com.jet.goods.domain.query.CategoryListQuery;
import com.jet.goods.domain.query.CategoryQuery;
import com.jet.goods.domain.vo.CategoryVo;
import com.jet.goods.service.IGoodsCategoryService;
import cn.hutool.core.util.StrUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/goods/category")
public class CategoryController {

    private final IGoodsCategoryService goodsCategoryService;

    public CategoryController(IGoodsCategoryService goodsCategoryService) {
        this.goodsCategoryService = goodsCategoryService;
    }

    @PostMapping
    public ResultJson<GoodsCategory> create(@RequestHeader long merchantNo,
                                            @RequestHeader long storeNo,
                                            @RequestHeader String accountId,
                                            @RequestBody @Validated CategoryDto dto) {
        return ResultJson.ok(goodsCategoryService.create(merchantNo, dto));
    }

    @PostMapping("/import")
    public ResultJson<Void> categoryImport(@RequestHeader long merchantNo,
                                           @RequestHeader long storeNo,
                                           @RequestHeader String accountId,
                                           @RequestParam("file") MultipartFile file) {
        goodsCategoryService.categoryImport(merchantNo, file);
        return ResultJson.ok();
    }

    @PutMapping("/{id}")
    public ResultJson<Void> edit(@RequestHeader long merchantNo,
                                 @RequestHeader long storeNo,
                                 @RequestHeader String accountId,
                                 @PathVariable("id") String id,
                                 @RequestBody CategoryDto dto) {
        goodsCategoryService.edit(merchantNo, id, dto);
        return ResultJson.ok();
    }

    @PutMapping("/sort/{id}")
    public ResultJson<Void> editSort(@RequestHeader long merchantNo,
                                     @RequestHeader long storeNo,
                                     @RequestHeader String accountId,
                                     @PathVariable("id") String id,
                                     @RequestBody CategoryDto dto) {
        goodsCategoryService.editSort(merchantNo, id, dto);
        return ResultJson.ok();
    }

    @DeleteMapping("/{id}")
    public ResultJson<Void> deleteById(@RequestHeader long merchantNo,
                                       @RequestHeader long storeNo,
                                       @RequestHeader String accountId,
                                       @PathVariable("id") String id) {
        goodsCategoryService.deleteById(merchantNo, id);
        return ResultJson.ok();
    }

    @GetMapping("/{id}")
    public ResultJson<GoodsCategory> info(@RequestHeader long merchantNo,
                                          @RequestHeader long storeNo,
                                          @RequestHeader String accountId,
                                          @PathVariable("id") String id) {
        return ResultJson.ok(goodsCategoryService.findById(merchantNo, id));
    }

    @GetMapping("/page")
    public ResultJson<PageVo<CategoryVo>> page(@RequestHeader long merchantNo,
                                               @RequestHeader long storeNo,
                                               @RequestHeader String accountId,
                                               CategoryQuery query) {
        PageVo<CategoryVo> result = goodsCategoryService.page(merchantNo, query);
        return ResultJson.ok(result);
    }

    @GetMapping("/list")
    public ResultJson<List<GoodsCategory>> list(@RequestHeader long merchantNo,
                                                @RequestHeader long storeNo,
                                                @RequestHeader String accountId,
                                                CategoryQuery query) {
        int level = CategoryLevel.TWO.getCode();
        if (StrUtil.isBlank(query.getPid())) {
            level = CategoryLevel.ONE.getCode();
        }

        CategoryListQuery categoryQuery = new CategoryListQuery();
        categoryQuery.setSearchKey(query.getSearchKey());
        categoryQuery.setPid(query.getPid());
        categoryQuery.setLevel(level);
        return ResultJson.ok(goodsCategoryService.findAll(merchantNo, categoryQuery));
    }
}
